$(function () {
    // 注册、登录页面切换
    $('#reg_link').on('click', function () {
        $('.login-box').hide().siblings('.reg-box').show();
    });
    $('#login_link').on('click', function () {
        $('.login-box').show().siblings('.reg-box').hide();
    });
    //自定义校验规则
    layui.form.verify({
        pwd: [/^\S{6,12}$/, '密码是6-12位非空格'],
        //value是用户输入的值
        repwd: function (value) {
            var pwd = $('.reg-box [name=password').val()
            if (pwd != value) {
                return '两次密码不一致!'
            }
        }
    });
    //注册功能
    var layer = layui.layer;
   // var {layer} = layui;同理 var layer = layui.layer;
    $('#reg_form').on('submit',function(e){
        e.preventDefault();
        var username = $('#reg_form [name="username"]').val();
        var password = $('#reg_form [name="password"]').val();
        $.post('/api/reguser',{username:username,password:   password},function(res){
            if (res.status != 0)return layer.msg(res.message)
            layer.msg(res.message);
            //跳转到登录表单，模拟点击
            $('#login_link').click();
        })
    });
    //登录功能
    $('#login_form').on('submit',function(e){
        e.preventDefault();
        $.ajax({
            type:'post',
            url:'/api/login',
            data:$('#login_form').serialize(),
            success:function(res){
                if (res.status != 0){
                    return layer.msg(res.message);
                }
                layer.msg(res.message);
                localStorage.setItem('token',res.token);
                //跳转到首页
                location.href = 'index.html';
            }
        })
    });




})